home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.001 / crossfir~ / eutl / errlib / errlib.h < prev    next >
C/C++ Source or Header  |  1994-10-07  |  5KB  |  112 lines

  1. #ifndef __errlib_h
  2. #define __errlib_h
  3.  
  4. #include <errno.h>
  5. #include <setjmp.h>
  6. /* The Package Variable should be a constant variable so that a single
  7.    Error function could handle errors from multiple packages. 
  8.    Error functions are officially defined as never returning.
  9.    They MUST either abort the program, or longjmp outside of the calling
  10.    function.  This is done because it makes all the error handling in 
  11.    libraries much MUCH easier, because you can nest functions in your 
  12.    library and just assume that everything is proceeding as planned.
  13.    An implementation of exceptions is handled which allows for slightly
  14.    easier handling of the setjmp longjmp problem.  The implementation of
  15.    exceptions is my own but is based on something I read in Byte magazine
  16.    a very long time ago. */
  17.  
  18. /* If the error message is longer than 1023 characters, this code should
  19.    abort.  I don't know of any way of providing printf style functionality
  20.    into a string without possibly overflowing the end. */
  21.  
  22. /* If you want to store the error message that you recieve inside an
  23.    exception handler, you will need to save it in a malloced buffer, 
  24.    because it will be destroyed on the next call to the error handling
  25.    functions */
  26.  
  27. typedef void (*ErrorFunction)(char *package,char *errid,char *errfmt,...);
  28.  
  29. /* This error function prints out the package and the msg format and
  30.    then aborts. This should NOT be the default error function for libraries,
  31.    since that would force people to always reset the error function and
  32.    the LongJumpPrintErrorFunction provides almost exactly the same 
  33.    functionality in the case where the user doesn't handle the error. */
  34. void AbortErrorFunction(char *package,char *errid,char *errfmt, ...);
  35.  
  36. /* This error function prints out a warning and then longjumps back to 
  37.    an outside handler.  If there is no outside handler, it prints out 
  38.    a message about unhandled exception and aborts */
  39. void LongJmpPrintErrorFunction(char *package,char *errid,char *errfmt, ...);
  40.  
  41. /* This error function just builds up the exception structure and then
  42.    longjmps to a handler.  If there isn't any handler, it will print out
  43.    a message about an unhandled exception and abort */
  44. void LongJmpErrorFunction(char *package,char *errid,char *errfmt, ...);
  45.  
  46. /* Definitions for exception handling. */
  47.  
  48. typedef struct __exceptionStruct {
  49.   struct __exceptionStruct *cur;
  50.   struct __exceptionStruct *next;/* For consistancy checking */
  51.   jmp_buf towhere;
  52. } exceptionStruct;
  53.  
  54. /* You can examine these values, but you should not modify them.  They are
  55.    guarenteed to be non-NULL after a call to throw. */
  56.  
  57. extern char *exception_progname;
  58. extern char *exception_package;
  59. extern char *exception_eid; /* Error Id, made a char * so that changes in error
  60.                 id's will be checked at link time */
  61. extern char *exception_errmsg;
  62.  
  63. void errlib_setprogname(char *progname);
  64.  
  65. void throwerr(char *package,char *eid,char *buf);
  66.  
  67. #define WITH_HANDLING { exceptionStruct __excstr;\
  68.               __excstr.cur = &__excstr;\
  69.               __excstr.next = __errlib_top;\
  70.               __errlib_top = &__excstr;\
  71.               if (setjmp(__excstr.towhere)==0) {
  72. #define CLEANUP_HANDLING __errlib_top = __errlib_top->next; 
  73. #define HANDLE CLEANUP_HANDLING } else {
  74. #define END_HANDLING CLEANUP_HANDLING }}
  75. #define BEGIN_MATCH if (0) {
  76. #define EXCP_MATCH(package,eid) } else if ((package)==exception_package &&\
  77.                        (eid)==exception_eid) {
  78. #define XMATCH(pname,eid) EXCP_MATCH(pname ## _packagever,pname ## _E ## eid)
  79. #define RERAISE() { CLEANUP_HANDLING;\
  80.                   throwerr(exception_package,exception_eid,exception_errmsg);}
  81. #define PROTECT_UNWIND RERAISE(); }}
  82. #define END_MATCH } else RERAISE();
  83. /* Simple Handle */
  84. #define SHANDLE1(do,pname,eid,handle) \
  85. WITH_HANDLING { \
  86.   do; \
  87. } HANDLE { \
  88.   BEGIN_MATCH; \
  89.   XMATCH(pname,eid) { \
  90.     handle; \
  91.   } \
  92.   END_MATCH; \
  93. } END_HANDLING; 
  94.  
  95. /* Define this for machines which don't have it by default */
  96. #ifdef sparc
  97. char *strerror(int errno);
  98. #endif
  99.  
  100. extern char *errlib_packagever,*errlib_EOverflowBuffer;
  101.  
  102. extern char *internal_Einternal; 
  103. /* This is for some sort of internal error that shouldn't have occurred, and
  104.    probably means something has stomped on memory or something like that */
  105.  
  106. /* DO NOT MODIFY THIS VARIABLE UNLESS YOU REALLY REALLY 
  107.    UNDERSTAND WHAT YOU ARE DOING, I.E. YOU HAVE READ THE CODE THAT MODIFIES
  108.    THIS AND YOU UNDERSTAND WHAT THAT CODE IS TRYING TO DO! */
  109. extern exceptionStruct *__errlib_top;
  110.  
  111. #endif /*ndef __errlib_h*/
  112.